package top.drinyeit.nongmao.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.presenter.RechargePresenter;

/**
 * Created by wu on 2018/1/6.
 */

public class RechargeActivity extends BaseActivity {

    private EditText number, password;
    private TextView recharge;
    private ImageView delete_all1, delete_all0;

    private RechargePresenter rechargePresenter;
    double price = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recharge);

        number = findViewById(R.id.number);
        password = findViewById(R.id.password);
        recharge = findViewById(R.id.recharge);
        delete_all1 = findViewById(R.id.delete_all1);
        delete_all0 = findViewById(R.id.delete_all0);

        initData();
        init();

        setTitle(true, "充值");
    }

    public void initData() {
        price = getIntent().getDoubleExtra("price", 0);
        rechargePresenter = new RechargePresenter(this);
    }

    @Override
    public void init() {
        recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (number.getText().length() == 0) {
                    showToastMsg("卡号不能为空");
                    return;
                }
                if (password.getText().length() == 0) {
                    showToastMsg("请输入密码");
                    return;
                }
                rechargePresenter.recharge(number.getText().toString(), password.getText().toString());
            }
        });

        number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    delete_all0.setVisibility(View.VISIBLE);
                } else {
                    delete_all0.setVisibility(View.GONE);
                }
            }
        });

        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    delete_all1.setVisibility(View.VISIBLE);
                } else {
                    delete_all1.setVisibility(View.GONE);
                }
            }
        });

        delete_all0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number.setText("");
            }
        });
        delete_all1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                password.setText("");
            }
        });
    }
}
